package com.legolas.session;

import com.legolas.utils.WebUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//自定义拦截器,Session过期AJAX处理
public class SessionExpiredFilter extends PathMatchingFilter {

    @Override
    protected boolean onPreHandle(ServletRequest servletRequest, ServletResponse servletResponse, Object mappedValue) throws Exception {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String user = (String) SecurityUtils.getSubject().getSession().getAttribute("user_info");
        if(user==null){
            if (WebUtil.isAjax(request)) {
                response.sendError(401);
                return false;
            }
            else{
                //普通的处理，直接给到下一个拦截器
                return true;
            }
        }

        return true;
    }


}